package com.chenhan.system.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chenhan.common.core.constant.Constants;
import com.chenhan.common.core.constant.UserConstants;
import com.chenhan.common.core.utils.StreamUtils;
import com.chenhan.system.domain.SysMenu;
import com.chenhan.system.domain.SysRole;
import com.chenhan.system.domain.SysRoleMenu;
import com.chenhan.system.domain.SysUserRole;
import com.chenhan.system.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Set;

@Service
@RequiredArgsConstructor
public class PermissionServiceImpl2 implements PermissionService2 {

    private final UserRoleService2 userRoleService2;
    private final RoleService2 roleService2;

    private final RoleMenuService2 roleMenuService2;

    private final MenuService2 menuService2;


    /**
     * 获取角色数据权限
     *
     * @return 角色权限信息
     */
    public Set<Long> getRoleIds(Long userId,Integer status) {
        List<SysUserRole> list = userRoleService2.list(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId, userId));
        Set<Long> roleIds = StreamUtils.convertSet(list, SysUserRole::getRoleId);

        // 查询此状态的roleId集合
        List<SysRole> roleDOList = roleService2.list(Wrappers.<SysRole>lambdaQuery()
                .select(SysRole::getRoleId)
                .eq(SysRole::getStatus, status));
        Set<Long> statusRoleIds = StreamUtils.convertSet(roleDOList, SysRole::getRoleId);
        // 取交集
        roleIds.retainAll(statusRoleIds);
        return roleIds;
    }

    public List<String> getRoleKeys(Collection<Long> roleIds) {
        return roleService2.selectRoleKeysByRoleIds(roleIds);
    }

    public List<String> getRoleKeys(Long userId,Integer status) {
        Set<Long> roleIds = getRoleIds(userId,status);
        return getRoleKeys(roleIds);
    }

    public List<SysMenu> getMenus(Collection<Long> roleIds, Collection<String> menuTypes, Integer status) {
        List<SysRole> roleDOs = roleService2.list(Wrappers.<SysRole>lambdaQuery().in(SysRole::getRoleId, roleIds));
        Set<String> roleKeys = StreamUtils.convertSet(roleDOs, SysRole::getRoleKey);
        if(roleKeys.contains(UserConstants.SUPER_ADMIN)){
            //  超管:
            return menuService2.list(Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getStatus, status).in(SysMenu::getMenuType,menuTypes));
        }else{
            List<SysRoleMenu> roleMenuDOS = roleMenuService2.list(Wrappers.<SysRoleMenu>lambdaQuery().in(SysRoleMenu::getRoleId, roleIds));
            Set<Long> menuIds = StreamUtils.convertSet(roleMenuDOS, SysRoleMenu::getMenuId);
            List<SysMenu> menus = menuService2.listByIds(menuIds);
            return StreamUtils.filterList(menus,menu -> status.equals(menu.getStatus()) && menuTypes.contains(menu.getMenuType()));
        }
    }

    public List<SysMenu> getMenus(Long userId,Collection<String> menuTypes,Integer status) {
        Set<Long> roleIds = getRoleIds(userId, status);
        return getMenus(roleIds,menuTypes,status);
    }


    public List<String> getPermCode(Collection<Long> roleIds) {
        List<SysMenu> menus = getMenus(roleIds, CollUtil.newHashSet(UserConstants.TYPE_MENU,UserConstants.TYPE_BUTTON), Constants.ENABLE);
        return StreamUtils.convertListWithDistinct(menus,SysMenu::getPerms);
    }
    public List<String> getPermCode(Long userId) {
        Set<Long> roleIds = getRoleIds(userId,Constants.ENABLE);
        return getPermCode(roleIds);
    }



}
